package actors;

import models.TienePersona;
import models.users.User;
import play.libs.Akka;
import services.DAO;
import services.PasswordHash;
import util.PermissionValidator;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;

public class UserCreator extends UntypedActor {
	public static final ActorRef instance = Akka.system().actorOf(
			new Props(UserCreator.class));

	@Override
	public void onReceive(Object arg0) throws Exception {
		AuthData ac = (AuthData) arg0;

		// Mailer.sendHtmlMail("Nombre de usuario y contraseña",
		// views.html.mailUsernameAndPassword.apply(ac._1(), ac._2())
		// .body(), ac._3());
	}

	public static void createUser(TienePersona c) {
		String username = PermissionValidator.generateUsername(c);
		String password = PermissionValidator.generatePassword(c);
		String email = c.getPersona().getEmail();
		AuthData authData = new AuthData(username, password, email);
		User u = new User();
		u.setId(c.getPersona().getId());
		u.setUsername(username);
		u.setPassword(PasswordHash.createHash(password));

		DAO.persist(u);

		instance.tell(authData, instance);
	}

	private static class AuthData {
		private String username;
		private String password;
		private String email;

		public AuthData(String username, String password, String email) {
			super();
			this.setUsername(username);
			this.setPassword(password);
			this.setEmail(email);
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

	}

}
